package com.xiu.travel.util;

/**
 * String工具
 *
 * @author : qianlanyuyun
 * @since : 2023/3/27 11:20
 */

public class StringUtil
{
    /**
     * 定义下划线
     */
    private static final char UNDERLINE = '_';

    /**
     * String为空判断(不允许空格)
     */
    public static boolean isBlank(String str)
    {
        return str == null || "".equals(str.trim());
    }

    /**
     * String不为空判断(不允许空格)
     */
    public static boolean isNotBlank(String str)
    {
        return !isBlank(str);
    }

    /**
     * 去除null
     *
     * @param str 字符串
     * @return 空字符串或原文
     */
    public static String excludeEmpty(String str)
    {
        return str == null ?
                "" :
                str;
    }

    /**
     * 判空后替换
     *
     * @param str    是否为空
     * @param newStr 为空的替换
     * @return String
     */
    public static String isBlankReplace(String str, String newStr)
    {
        return (str == null || "".equals(str.trim()) || "null".equals(str.trim())) ?
                newStr :
                str;
    }

    /**
     * Byte数组为空判断
     */
    public static boolean isNull(byte[] bytes)
    {
        // 根据byte数组长度为0判断
        return bytes == null || bytes.length == 0;
    }

    /**
     * Byte数组不为空判断
     */
    public static boolean isNotNull(byte[] bytes)
    {
        return !isNull(bytes);
    }

    /**
     * 驼峰转下划线工具
     */
    public static String camelToUnderline(String param)
    {
        if ( isNotBlank(param) )
        {
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for ( int i = 0; i < len; i++ )
            {
                char c = param.charAt(i);
                if ( Character.isUpperCase(c) )
                {
                    sb.append(UNDERLINE);
                    sb.append(Character.toLowerCase(c));
                } else
                {
                    sb.append(c);
                }
            }
            return sb.toString();
        } else
        {
            return "";
        }
    }

    /**
     * 下划线转驼峰工具
     */
    public static String underlineToCamel(String param)
    {
        if ( isNotBlank(param) )
        {
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for ( int i = 0; i < len; i++ )
            {
                char c = param.charAt(i);
                if ( c == 95 )
                {
                    ++i;
                    if ( i < len )
                    {
                        sb.append(Character.toUpperCase(param.charAt(i)));
                    }
                } else
                {
                    sb.append(c);
                }
            }
            return sb.toString();
        } else
        {
            return "";
        }
    }

    /**
     * 在字符串两周添加''
     */
    public static String addSingleQuotes(String param)
    {
        return "\'" + param + "\'";
    }

    /**
     * 判断字符串是否存在结果集当中
     *
     * @param param     参数
     * @param condition 条件
     * @return true:存在
     */
    public static boolean existence(String param, String... condition)
    {
        for ( String s : condition )
        {
            if ( param.equals(s) )
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断字符串类型
     *
     * @param param     参数
     * @param condition 条件
     * @return true:存在
     */
    public static boolean existenceEnds(String param, String... condition)
    {
        for ( String s : condition )
        {
            if ( param.endsWith(s) )
            {
                return true;
            }
        }
        return false;
    }

    //首字母转小写
    public static String toLowerCaseFirstOne(String s)
    {
        if ( Character.isLowerCase(s.charAt(0)) )
        {
            return s;
        } else
        {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    //首字母转大写
    public static String toUpperCaseFirstOne(String s)
    {
        if ( Character.isUpperCase(s.charAt(0)) )
        {
            return s;
        } else
        {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }
}

